package com.github_dididipapa.mallsearch.controller;

import com.alibaba.fastjson2.JSON;
import com.github_dididipapa.common.exception.BizCodeEnume;
import com.github_dididipapa.common.exception.ElasticSearchException;
import com.github_dididipapa.common.to.es.SkuEsModel;
import com.github_dididipapa.common.utils.R;
import com.github_dididipapa.mallsearch.constant.ElasticSearchOptionsConstant;
import com.github_dididipapa.mallsearch.service.ProductService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.xcontent.XContentType;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * @description: ES 控制器
 * @author: dididi_papa
 * @email: mitis1624087624@gmail.com
 * @date: 2025/07/02  11:59
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("search/save")
public class ElasticSearchSaveController {

    private final ProductService productService;

    @PostMapping(value = "/product", produces =  MediaType.APPLICATION_JSON_VALUE)
    public R saveSpu(@RequestBody List<SkuEsModel> esModelList) {
        boolean flag = false;
        try {
            flag = productService.saveSpu(esModelList);
        } catch (IOException e) {
            log.error("ES商品上架失败:{}", e.getMessage());
            throw new RuntimeException(e);
        }

        return flag ?
                R.ok() :
                R.error(BizCodeEnume.PRODUCT_UP_EXCEPTION.getCode(), BizCodeEnume.PRODUCT_UP_EXCEPTION.getMsg());
    }
}
